/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 21626
 * Date: 2024-11-03
 * Time: 11:51
 */

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Test {
    public int longestPalindrome(String s) {
        int[] str = new int[128];
        int ret = 0;
        for(int i = 0; i < s.length() ; i++){
            //将字母转换为ASCLL码作为下标存进str
            str[s.charAt(i)]++;
        }
        for(int x : str){
            ret += x / 2 * 2;
        }
        //回文长度小于原字符串长度，说明存在奇数字符，作为中心
        return ret < s.length() ? ret + 1 : ret;
    }
    public String largestNumber(int[] nums) {
        int n = nums.length;
        String[] strs = new String[n];
        for(int i = 0; i < n ; i++){
            strs[i] = "" + nums[i];
        }
        Arrays.sort(strs, (a, b) ->
        {
            return (b+a).compareTo(a+b);
        });
        StringBuffer ret = new StringBuffer();
        for(String s : strs){
            ret.append(s);
        }
        if(ret.charAt(0) == '0'){return "0";}
        return ret.toString();
    }
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) {
                return true;
            }
            map.put(nums[i], i);
            if (map.size() > k) {
                map.remove(nums[i - k]);
            }
        }
        return false;
    }
    public int findLHS(int[] nums) {
        Map<Integer,Integer> hasMap = new HashMap<>();
        for(int num : nums){
            hasMap.put(num,hasMap.getOrDefault(num,0) + 1);
        }
        int longest = 0;
        for(int num:hasMap.keySet()){
            if(hasMap.containsKey(num+1)){
                longest = Math.max(longest,hasMap.get(num)+hasMap.get(num+1));
            }
        }
        return longest;
    }
    public int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        int[] dist = new int[n];
        Stack<Integer> indexs = new Stack<>();
        for(int curIndex = 0; curIndex < n; curIndex++){
            while(!indexs.isEmpty() && (temperatures[curIndex] > temperatures[indexs.peek()])){
                int preIndex = indexs.pop();
                dist[preIndex] = curIndex - preIndex;
            }
            indexs.add(curIndex);
        }
        return dist;
    }
}
